package com.quick.develop.flowable.utils;

import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.ExtensionElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 扩展字段获取公共类
 * @author hyd on 2022/12/27.
 * @version 1.0
 */
public class ExtensionElementUitls {


    /**
     * 获取扩展字段的list
     * @param extensionElements
     * @return
     */
    public static List<ExtensionElement>   getChildExtensionList(Map<String, List<ExtensionElement>> extensionElements){
//         childExtensionElements = new LinkedHashMap<>();
        List<ExtensionElement> listChild = new ArrayList<>();
        if(extensionElements!=null){
            List<ExtensionElement> list = extensionElements.get("properties");
            if(list!=null && list.size()>0){
                ExtensionElement extensionElement = list.get(0);
                Map<String, List<ExtensionElement>> childExtensionElements = extensionElement.getChildElements();
                if(childExtensionElements!=null){
                    listChild = childExtensionElements.get("property");
                }
            }
        }
        return listChild;
    }

    /**
     * 根据扩展字段名 称获取扩展字段value
     * @param listChild
     * @param strName
     * @return
     */
    public static String getEleMentStr(List<ExtensionElement> listChild,String strName){
        String value = null;
        for(ExtensionElement obj:listChild){
            Map<String, List<ExtensionAttribute>> attributes = obj.getAttributes();
            List<ExtensionAttribute> listName = attributes.get("name");
            ExtensionAttribute extensionAttributeName = listName.get(0);
            if(strName.equals(extensionAttributeName.getValue())){
                List<ExtensionAttribute> listValue = attributes.get("value");
                ExtensionAttribute extensionAttributeValue = listValue.get(0);
                value = extensionAttributeValue.getValue();
                break;
            }
            continue;
        }
        return value;
    }


    /**
     * 根据扩展字段名称获取 value
     * @param extensionElements
     * @param strName
     * @return
     */
    public static String getEleMentStr(Map<String, List<ExtensionElement>> extensionElements,String strName){
        String value = null;
        List<ExtensionElement> listChild = getChildExtensionList(extensionElements);
        value = getEleMentStr(listChild,strName);
        return value;
    }


}
